package controllers

import (
	"github.com/astaxie/beego"
	"wego/models"
)

type MainController struct {
	beego.Controller
}

//type JSONStruct struct {
//	Code int
//	Msg  string
//}

type UserInfo struct {
	Nickname string
	Age      int
	Sex      int
}

func (this *MainController) Index() {

	//sortorder := "Id"
	//query := orm.NewOrm().QueryTable("tp_menu")
	//list := make([]*models.Menu, 0)
	//_, _ = query.OrderBy(sortorder).All(&list)
	//tree := make([]interface{},0)
	//refer := make(map[int64]interface{}, 0)
	//for _, item := range list {
	//	refer[item.Id] = &item
	//}
	//
	//for _, item := range list {
	//	parentId := item.Fid
	//	if parentId == 0 {
	//		tree = append(tree,item)
	//	} else {
	//		if refer[parentId] != nil {
	//			var parent = *refer[parentId]
	//		}
	//	}
	//}
	//
	//this.Data["json"] = &list
	//this.ServeJSON()

	//s := "[1, 2, 3, 4]"
	//var a []int
	//// 将字符串反解析为数组
	//_ = json.Unmarshal([]byte(s), &a)
	//fmt.Println(a)      // [1 2 3 4]
	//a := strings.Split("1,2,3,4", ",")
	//this.Data["json"] = &a
	//this.ServeJSON()
	//o := orm.NewOrm()
	//var maps []orm.Params
	//query := o.QueryTable("tp_attach")
	//_, _ = query.Filter("id__in", 1,2,3,12).Values(&maps, "Path", "SaveType", "QnKey")

	//for _, item := range maps {
	//	if item["SaveType"] == 1 {
	//		//this.Ctx.WriteString(item["SaveType"].(string))
	//
	//	} else {
	//		i := item["SaveType"]
	//		this.Data["json"] = i == 1
	//		this.ServeJSON()
	//	}
	//}
	//
	//this.Data["json"] = maps
	//this.ServeJSON()

	//mystruct := &JSONStruct{0, "hello"}
	//	//this.Data["json"] = mystruct
	//	//user := &UserInfo{"yann",18,1}
	//	//this.Data["json"] = user
	//	//this.ServeJSON()

	//o := orm.NewOrm()
	//admin := models.Admin{Id: 10}
	//err := o.Read(&admin)
	//if err == orm.ErrNoRows {
	//	this.Ctx.WriteString("查询不到")
	//} else if err == orm.ErrMissPK {
	//	this.Ctx.WriteString("找不到主键")
	//} else {
	//	this.Data["json"] = admin
	//	this.ServeJSON()
	//}

	//controllerName, actionName := this.GetControllerAndAction()
	//result := make(map[string]interface{})
	//result["controllerName"] = controllerName
	//result["actionName"] = actionName
	//this.Data["json"] = result

	//result := make(map[string]interface{})
	//err := utils.GetCache("QWI5FrHUMc7sGZngYND5gJ9m9S3tedny", &result)
	//if err == nil {
	//	res := make(map[string]interface{})
	//	res["data"] = result
	//	res["err"] = err
	//	this.Data["json"] = res
	//	this.ServeJSON()
	//} else {
	//	this.Data["json"] = err
	//	this.ServeJSON()
	//}

	//o := orm.NewOrm()
	//r := models.AuthGroupAccess{Uid: 10}
	//er := o.Read(&r)
	//
	//if er != nil {
	//	this.Data["json"] = &r
	//	this.ServeJSON()
	//}

	//list := models.RuleList(1)
	//list,_ := models.GetAuthByGroupId(2)

	//o := orm.NewOrm()
	//var list []models.AuthGroupAccess
	//sql := fmt.Sprintf(`SELECT * FROM %s Where id LIKE ?`, "tp_auth_group_access")
	//_, _ = o.Raw(sql, 1).QueryRows(&list)

	//qb, _ := orm.NewQueryBuilder("mysql")
	//qb.Select("id", "uid", "group_id").
	//	From("tp_auth_group_access").
	//	Where("group_id like '%1%'")
	//sql := qb.String()

	// 执行 SQL 语句
	//o := orm.NewOrm()
	//_, _ = o.Raw(sql, 1).QueryRows(&list)
	//cond := orm.NewCondition()
	//conds := cond.Or(
	//	"group_id__contains","," + strconv.Itoa(2) +",").Or(
	//	"group_id__startswith","[" + strconv.Itoa(2) + ",").Or(
	//	"group_id__endswith", "," + strconv.Itoa(2) + "]").Or("group_id__exact","[" + strconv.Itoa(2) + "]")
	//_, _ = orm.NewOrm().QueryTable("tp_auth_group_access").SetCond(conds).All(&list)

	//_, _ = o.Raw(sql, 1).QueryRows(&list)

	access, _ := models.GetAuthByLogin("[1,2]")

	this.Data["json"] = access
	this.ServeJSON()
}
